package com.prabhakar.json;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class ConsumeJson {

	public void consumeJsonArray(JSONArray jsonArray){
		
		System.out.println("jsonArray recevd in ConsumeJson class >> \n"+jsonArray);

		for(int i=0; i< jsonArray.size() ;i++){

			JSONObject jsonObj = jsonArray.getJSONObject(i);
		//	System.out.println("\n jsonObj.toString() from consumeJsonArray "+jsonObj.toString());
			
		//	DemoBean beanObj = jsonToJavaConversion(jsonObj.toString());
		//	printBeanObjValues(beanObj);
			
			jsonToJavaConversion(jsonObj);
		}
	
	}

	private void jsonToJavaConversion(JSONObject jsonObj) {
		// TODO Auto-generated method stub
		DemoBean beanObj = new DemoBean();
		
		System.out.println("jsonObj recevd in jsonToJavaConversion  >> "+jsonObj.toString());
		
		beanObj.setName(jsonObj.get("name").toString());
		beanObj.setName(jsonObj.getString("phonenumber"));
		beanObj.setEmail(jsonObj.getString("address"));
		beanObj.setName(jsonObj.getString("requestDate"));
		
		/*if(!jsonObj.isEmpty()){
			if(!jsonObj.getString("name").isEmpty()) 	beanObj.setName(jsonObj.getString("name"));
			if(!jsonObj.getString("phonenumber").isEmpty()) 	beanObj.setName(jsonObj.getString("phonenumber"));
			if(!jsonObj.getString("address").isEmpty())    beanObj.setEmail(jsonObj.getString("address"));
			if(!jsonObj.getString("requestDate").isEmpty()) 	beanObj.setName(jsonObj.getString("requestDate"));
		}*/
		
		printBeanObjValues(beanObj);
		
		
		
	}
	
	private void printBeanObjValues(DemoBean beanObj) {

	//	if(beanObj!=null){
			System.out.println("\n-----------------------------------------------");
			System.out.println(" name retrieved from bean >>> "+beanObj.getName());
			System.out.println(" phonenumber retrieved from bean >>> "+beanObj.getPhonenumber());
			System.out.println(" email retrieved from bean >>> "+beanObj.getEmail());
			System.out.println(" address retrieved from bean >>> "+beanObj.getAddress());
			System.out.println(" date retrieved from bean >>> "+beanObj.getRequestDate());
			System.out.println("\n-----------------------------------------------");
	//	}
		
	}

	
	
}
